
//#include "StateShop.h"
#include "StateShopAnimals.h"
#include "StateShopAnimalDetail.h"
//#include "StateShopUpgrades.h"
//#include "StateShopPowerUps.h"
#include "StatePopUp.h"
#include "StateCoinsPopUp.h"

#include "StateMainMenu.h"
#include "StateManager.h"
#include "InputManager.h"
#include "AudioUtils.h"
#include "ProgressManager.h"
#include "LevelManager.h"

void StateShopAnimalDetail::loadContent()
{
    StateShopCommon::loadContent();
    
    m_oPassport = new RenderableEntity("passport", Vector2(320, 500));
    m_oPassport->setCentered();

    for(int i=0; i<MAX_BABY_TYPES; ++i)
    {
        BabyData sData = LevelManager::instance().getBabyData(i);
        
        m_oBaby[i] = new AnimatedEntity(sData.sName, Vector2(185, 585));
        m_oBaby[i]->setScale(Vector2(1.3f, 1.3f));
        m_oBaby[i]->setCentered();
        
        m_oFood[i] = new AnimatedEntity(sData.sFood, Vector2(530, 640));
        m_oFood[i]->setScale(Vector2(1.0f, 1.0f));
        m_oFood[i]->setCentered();
        if(!sData.bFoodAnimated)
        {
            m_oFood[i]->setLiving(true);
            m_oFood[i]->setLivingIntensity(Vector2(1.0f, 1.0f));
            m_oFood[i]->setLivingSpeed(Vector2(1.0f, 1.0f));
        }

        m_oPrize[i] = new RenderableEntity(sData.sPrize, Vector2(530, 510));
        m_oPrize[i]->setScale(Vector2(0.7f, 0.7f));
        m_oPrize[i]->setLiving(true);
        m_oPrize[i]->setCentered();
        
        m_oEnter->addInEffect(m_oBaby[i]);
        m_oEnter->addInEffect(m_oFood[i]);
        m_oEnter->addInEffect(m_oPrize[i]);

        m_oExit->addOutEffect(m_oBaby[i]);
        m_oExit->addOutEffect(m_oFood[i]);
        m_oExit->addOutEffect(m_oPrize[i]);
    }
    
    m_oEats = new TextEntity("Eats", FONT_NAME, 36, Vector2(450, 660), 2, 1);
    m_oEats->setColor(COLOR_BLACK);
    m_oEats->setShadow(true, COLOR_GREY);
    m_oEats->setCenter(Vector2(1.0f ,0.5f));
    
    m_oGives = new TextEntity("Gives", FONT_NAME, 36, Vector2(450, 510), 2, 1);
    m_oGives->setColor(COLOR_BLACK);
    m_oGives->setShadow(true, COLOR_GREY);
    m_oGives->setCenter(Vector2(1.0f ,0.5f));
    
    //Food Coind
    m_oFoodText = new TextEntity("Coins per Feed", FONT_NAME, 32, Vector2(80, 370), 0, 1);
    m_oFoodText->setColor(COLOR_BLACK);
    
    m_oFoodValue = new TextEntity(" 0", FONT_NAME, 32, Vector2(500, 370), 2, 1);
    m_oFoodValue->setCenter(Vector2(1.0, 0.0));
    m_oFoodValue->setColor(COLOR_BLACK);
    m_oFoodValue->setShadow(true, COLOR_GREY);
    
    m_oFoodCoin = new RenderableEntity("icon_babycoin", Vector2(500, 370));
    m_oFoodCoin->setScale(Vector2(0.5, 0.5));
    
    //Prize coins
    m_oPrizeText = new TextEntity("Coins per Prize", FONT_NAME, 32, Vector2(80, 300), 0, 1);
    m_oPrizeText->setColor(COLOR_BLACK);
    
    m_oPrizeValue = new TextEntity("0", FONT_NAME, 32, Vector2(500, 300), 2, 1);
    m_oPrizeValue->setCenter(Vector2(1.0, 0.0));
    m_oPrizeValue->setColor(COLOR_BLACK);
    m_oPrizeValue->setShadow(true, COLOR_GREY);
    
    m_oPrizeCoin = new RenderableEntity("icon_babycoin", Vector2(500, 300));
    m_oPrizeCoin->setScale(Vector2(0.5, 0.5));
    
    m_oEnter->addInEffect(m_oPassport);
    m_oEnter->addInEffect(m_oEats);
    m_oEnter->addInEffect(m_oGives);
    m_oEnter->addInEffect(m_oFoodText);
    m_oEnter->addInEffect(m_oFoodValue);
    m_oEnter->addInEffect(m_oPrizeText);
    m_oEnter->addInEffect(m_oPrizeValue);
    m_oEnter->addInEffect(m_oFoodCoin);
    m_oEnter->addInEffect(m_oPrizeCoin);

    m_oExit->addOutEffect(m_oPassport);
    m_oExit->addOutEffect(m_oEats);
    m_oExit->addOutEffect(m_oGives);
    m_oExit->addOutEffect(m_oFoodText);
    m_oExit->addOutEffect(m_oFoodValue);
    m_oExit->addOutEffect(m_oPrizeText);
    m_oExit->addOutEffect(m_oPrizeValue);
    m_oExit->addInEffect(m_oFoodCoin);
    m_oExit->addInEffect(m_oPrizeCoin);

}

void StateShopAnimalDetail::unloadContent()
{
    StateShopCommon::unloadContent();
}

void StateShopAnimalDetail::activated()
{
    StateShopCommon::activated();
}

void StateShopAnimalDetail::enter()
{
    switch(m_ePopupState)
    {
        case SHOPPOPUP_BUY:
            if(StatePopUp::instance().getResult())
            {
                //Bought the baby!
                SaveGame sSaveGame = ProgressManager::instance().getSavedData();
                int iPrice = LevelManager::instance().getBabyData(m_iBaby).iPrice;
                sSaveGame.iCoins -= iPrice;
                sSaveGame.iCoinsSpent += iPrice;
                sSaveGame.sBabyData[m_iBaby].bUnlocked = true;
                ProgressManager::instance().setSavedData(sSaveGame);
            }
            break;
            
        default:
            break;
    }
    
    StateShopCommon::enter();
    
    //Set name
    m_oTitleText->setText(LevelManager::instance().getBabyData(m_iBaby).sCaption);

    BabyData sBabyData = LevelManager::instance().getBabyData(m_iBaby);
    PrizeData sPrizeData = LevelManager::instance().getPrizeData(sBabyData.sPrize);
    
    char buff[10];
    
    //Set coins
    sprintf(buff, "%d", (int) (sBabyData.iScore));
    m_oFoodValue->setText(buff);
    
    //Set prize coins
    sprintf(buff, "%d", (int) (sPrizeData.iScore));
    m_oPrizeValue->setText(buff);
}

void StateShopAnimalDetail::updatePaused(float dt)
{
    StateShopCommon::updatePaused(dt);
    
    if(m_oEnter->isFinished() && m_oExit->isFinished())
    {
        m_oFood[m_iBaby]->update();
        m_oBaby[m_iBaby]->update();
        m_oPrize[m_iBaby]->update();
    }
}

void StateShopAnimalDetail::update(float dt)
{
    StateShopCommon::update(dt);
    
    switch(m_eState)
    {
        case SHOP_IDLE:        
            break;
            
        default:
            break;
    }
}

void StateShopAnimalDetail::render()
{
    StateShopCommon::render();

    m_oPassport->render();
    
    m_oBaby[m_iBaby]->render();
    m_oFood[m_iBaby]->render();
    m_oPrize[m_iBaby]->render();
    
    m_oEats->render();
    
    m_oGives->render();
    
    m_oFoodText->render();
    m_oFoodValue->render();
    m_oFoodCoin->render();
    
    m_oPrizeText->render();
    m_oPrizeValue->render();
    m_oPrizeCoin->render();
}

void StateShopAnimalDetail::exit()
{
    StateShopCommon::exit();
}

void StateShopAnimalDetail::setBackState()
{
    m_oNextState = &StateShopAnimals::instance();
}
